package com.xscr.api.utils;

import java.lang.reflect.InvocationTargetException;
import java.text.SimpleDateFormat;

import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.SqlDateConverter;

/**
 * @author yinjg
 *
 */
public class CopyBeanUtil {

	/**.
	 * @param oldProductVO DealerProductObjectDTO
	 * @param buyMap ,List<Integer>
	 * @return DealerProductObjectDTO
	 */
	static {
	    //注册sql.date的转换器，即允许BeanUtils.copyProperties时的源目标的sql类型的�?允许为空
	    ConvertUtils.register(new SqlDateConverter(), java.util.Date.class);
	    //ConvertUtils.register(new SqlTimestampConverter(), java.sql.Timestamp.class);
	    //注册util.date的转换器，即允许BeanUtils.copyProperties时的源目标的util类型的�?允许为空
	    ConvertUtils.register(new  Converter() {

			@Override
			 public Object convert(Class arg0, Object arg1) {
				if(arg1 instanceof java.util.Date){
					return arg1;
				}else if(arg1 instanceof String){
					String p = (String)arg1;
			        if(p== null || p.trim().length()==0){
			            return null;
			        }   
			        try{
			            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			            return df.parse(p.trim());
			        }
			        catch(Exception e){
			                SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
			                try {
								return df.parse(p.trim());
							} catch (java.text.ParseException e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
								return null;
							}
			        }
				}else{
		        	return null;
		        }
		        
		        
		    }

	    }, java.util.Date.class);
	  }
	
	
	
		public static <T> T copyProperties(T dest,Object src){
			try {
				org.apache.commons.beanutils.BeanUtils.copyProperties(dest, src);
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
			return dest;
		}
	}
